/**
 * @Author: ls <ls-mac>
 * @Date:   2019-10-13 13:53:52
 * @Last modified by:   ls-mac
 * @Last modified time: 2019-10-13 14:55:47
 */
import { dictPage, dictAddOrEdit, dictDelete } from 'services/dict';

export default {
  namespace: 'storeFeatures',
  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(dictPage, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },
    *dictAddOrEdit({ payload, callback }, { call, put }) {
      const response = yield call(dictAddOrEdit, payload);
      if (callback) callback(response);
    },
    *dictDelete({ payload, callback }, { call, put }) {
      const response = yield call(dictDelete, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { dictList, pageIndex, pageSize, recordCount },
      } = action.payload;

      const newList = dictList.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        data: {
          list: newList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },
  },
};
